package com.perfma.xtest.tool;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: junhui.si
 * @date: 2020-09-30 16:12
 **/
public class ExcelWriteDemo2 {

    public static void main(String[] args) {
        String templateFileName = TestFileUtil.getPath() + "excel-t.xlsx";
        String fileName = TestFileUtil.getPath() + "excel-t-1.xlsx";
        ExcelWriter excelWriter = null;
        try {
//            OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(10, 15, 2, 2);
//            EasyExcel.write(fileName, RemixSceneData.class).withTemplate(templateFileName)
//                    .sheet(2)
//                    .needHead(false)
//                    .doWrite(data());

            excelWriter = EasyExcel.write(fileName)
                    .withTemplate(templateFileName)
                    .build();

            WriteSheet writeSheet2 = EasyExcel.writerSheet(2).build();
            // 横向填充
            FillConfig horizontalFillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();

//            WriteTable table = new WriteTable();
//            table.setTableNo(1);
//            table.setHead(head(Lists.newArrayList(10, 20, 30)));

            excelWriter.fill(new FillWrapper("data1", data()), writeSheet2);
            excelWriter.fill(new FillWrapper("head", head2(Lists.newArrayList(10, 20, 30))), horizontalFillConfig, writeSheet2);

            List<Map<String,String>> list = Lists.newArrayList();
            Map<String,String> map = Maps.newHashMap();
            map.put("machine","{data4.machineData1}");
            list.add(map);
            map = Maps.newHashMap();
            map.put("machine","{data4.machineData2}");
            list.add(map);

            excelWriter.fill(new FillWrapper("data3", list), writeSheet2);

            list = Lists.newArrayList();
            map = Maps.newHashMap();
            map.put("machineData1","数据1");
            list.add(map);
            map = Maps.newHashMap();
            map.put("machineData2","数据2");
            list.add(map);
            excelWriter.fill(new FillWrapper("data4", list), horizontalFillConfig, writeSheet2);


//            excelWriter.write(data(), writeSheet2, table);
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    private static List<RemixSceneData> data() {
        List<RemixSceneData> list = Lists.newArrayList();
        RemixSceneData remixSceneData;
        for (int i = 0; i < 20; i++) {
            remixSceneData = new RemixSceneData();
            remixSceneData.setId("编号" + i);
            remixSceneData.setName("名字：" + i);
            remixSceneData.setTotalUserNums(10);
            list.add(remixSceneData);
        }
        return list;
    }

    private static List<Map<String, String>> machines() {
        Map<String, String> map;
        List<Map<String, String>> list = Lists.newArrayList();
        for (int i = 0; i < 6; i++) {
            map = new HashMap<>();
            map.put("machine", "服务器：" + i);
            list.add(map);
        }
        return list;
    }

    private static List<List<String>> head(List<Integer> usersList) {
        List<List<String>> headList = Lists.newArrayList();

        headList.add(Lists.newArrayList("服务器", "服务器"));
        for (Integer users : usersList) {
            String usersHead = users + "并发";
            headList.add(Lists.newArrayList(usersHead, "CPU"));
            headList.add(Lists.newArrayList(usersHead, "内存"));
        }
        return headList;
    }

    private static List<Map<String, String>> head2(List<Integer> usersList) {
        List<Map<String, String>> headList = Lists.newArrayList();

        Map<String, String> map;
        for (Integer users : usersList) {
            map = Maps.newHashMap();
            map.put("users", users + "并发");
            map.put("item", "CPU");
            headList.add(map);

            map = Maps.newHashMap();
            map.put("users", users + "并发");
            map.put("item", "内存");
            headList.add(map);
        }
        return headList;
    }

}
